package com.sjy.controller;

import com.sjy.controller.form.CodeNodeSaveForm;
import com.sjy.dao.CodeNodeRepository;
import com.sjy.dao.model.CodeNode;
import com.sjy.framework.*;
import com.sjy.framework.codemultiplex.NodeTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/codenode")
public class CodeNodeController {

    @Autowired
    private CodeNodeRepository codeNodeRepository;

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public ApiResult saveCodeNode(@RequestBody CodeNodeSaveForm form){
        try{
            CodeNode code = new CodeNode();
            code.setParentId("0");
            code.setId(SequenceGenerator.generateId());
            code.setNodeType(NodeTypeEnum.LEAF.getCode());
            code.setCodeContent(form.getCodeContent());
            code.setClassFullName(form.getClassFullName());
            code.setParamInfo(form.getParamInfo());
            code.setCreateTime(new Date().getTime());
            codeNodeRepository.save(code);
        }catch (Exception e){
            return  ApiResult.buildError(SystemErrorEnum.SYSTEM_ERROR.getError());
        }
        return ApiResult.buildSuccess(true);
    }
}
